package com.papers.juan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.utils.StringUtils;
import com.papers.juan.domain.TJuanGenerateDetail;
import com.papers.juan.domain.bo.TJuanGenerateDetailBo;
import com.papers.juan.domain.vo.TJuanGenerateDetailVo;
import com.papers.juan.mapper.TJuanGenerateDetailMapper;
import com.papers.juan.service.ITJuanGenerateDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 组卷明细Service业务层处理
 *
 * @author papers
 * @date 2022-11-02
 */
@RequiredArgsConstructor
@Service
public class TJuanGenerateDetailServiceImpl implements ITJuanGenerateDetailService {

    private final TJuanGenerateDetailMapper baseMapper;

    /**
     * 查询组卷明细
     */
    @Override
    public TJuanGenerateDetailVo queryById(String detailId) {
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询组卷明细列表
     */
    @Override
    public TableDataInfo<TJuanGenerateDetailVo> queryPageList(TJuanGenerateDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TJuanGenerateDetail> lqw = buildQueryWrapper(bo);
        Page<TJuanGenerateDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询组卷明细列表
     */
    @Override
    public List<TJuanGenerateDetailVo> queryList(TJuanGenerateDetailBo bo) {
        LambdaQueryWrapper<TJuanGenerateDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TJuanGenerateDetail> buildQueryWrapper(TJuanGenerateDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TJuanGenerateDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getGenerateId()), TJuanGenerateDetail::getGenerateId, bo.getGenerateId());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionType()), TJuanGenerateDetail::getQuestionType, bo.getQuestionType());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionEasy()), TJuanGenerateDetail::getQuestionEasy, bo.getQuestionEasy());
        lqw.eq(bo.getQuestionScore() != null, TJuanGenerateDetail::getQuestionScore, bo.getQuestionScore());
        lqw.eq(bo.getQuestionNum() != null, TJuanGenerateDetail::getQuestionNum, bo.getQuestionNum());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), TJuanGenerateDetail::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedTime() != null, TJuanGenerateDetail::getCreatedTime, bo.getCreatedTime());
        return lqw;
    }

    /**
     * 新增组卷明细
     */
    @Override
    public Boolean insertByBo(TJuanGenerateDetailBo bo) {
        TJuanGenerateDetail add = BeanUtil.toBean(bo, TJuanGenerateDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改组卷明细
     */
    @Override
    public Boolean updateByBo(TJuanGenerateDetailBo bo) {
        TJuanGenerateDetail update = BeanUtil.toBean(bo, TJuanGenerateDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TJuanGenerateDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除组卷明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
